import pathlib
import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager

# 基本信息
# 视频存放路径
catalog_mp4 = r"C:\Users\jingb\Desktop\图文生成\成大事的9种手段"
# 视频描述
describe = "裸眼3D看蜘蛛侠 #搞笑 #电影 #视觉震撼"
time.sleep(10)
# 创建Chrome浏览器服务
s = Service(ChromeDriverManager().install())
# 启动Chrome浏览器并打开指定网址
driver = webdriver.Chrome(service=s)

path = pathlib.Path(catalog_mp4)

# 视频地址获取
path_mp4 = ""
for i in path.iterdir():
    if ".mp4" in str(i):
        path_mp4 = str(i);
        break;

if path_mp4 != "":
    print("检查到视频路径：" + path_mp4)
else:
    print("未检查到视频路径，程序终止！")
    exit()

# 封面地址获取
path_cover = ""
for i in path.iterdir():
    if ".png" in str(i) or ".jpg" in str(i):
        path_cover = str(i);
        break;

if path_cover != "":
    print("检查到封面路径：" + path_cover)
else:
    print("未检查到封面路径，程序终止！")
    exit()


def publish_qutoutiao():
    '''
     作用：发布趣头条视频
    '''

    # 进入创作者页面，并上传视频
    driver.get("https://mp.qutoutiao.net/publish-content/video")
    time.sleep(2)
    driver.find_element(By.XPATH,'//input[@type="file"]').send_keys(path_mp4)

    # 等待视频上传完成
    while True:
        time.sleep(3)
        try:
            driver.find_element(By.XPATH,'//*[contains(text(),"上传成功")]')
            break;
        except Exception as e:
            print("视频还在上传中···")

    print("视频已上传完成！")

    # 输入标题
    driver.find_element(By.XPATH,'//*[@placeholder="内容标题5-30字"]').clear()
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@placeholder="内容标题5-30字"]').send_keys(describe)

    # 输入描述信息
    time.sleep(1)
    driver.find_element(By.XPATH,'//textarea').clear()
    time.sleep(2)
    driver.find_element(By.XPATH,'//textarea').send_keys(describe)

    # 选择分类
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@placeholder="请选择分类"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="电影"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="分类："]').click()
    time.sleep(1)

    # 输入标签
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').click()
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys("视觉震撼")
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys(Keys.ENTER)
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys("搞笑")
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys(Keys.ENTER)
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys("电影")
    time.sleep(2)
    driver.find_element(By.XPATH,'//*[@class="content-tag"]//input').send_keys(Keys.ENTER)

    # 添加封面
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[@class="el-upload"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="自定义封面"]').click()
    time.sleep(1)
    driver.find_element(By.XPATH,'//*[text()="选择图片"]/../..//input[@type="file"]').send_keys(path_cover)
    time.sleep(3)
    driver.find_element(By.XPATH,'//*[text()="确 定"]').click()

    # 人工进行检查并发布
    # time.sleep(3)
    # # 点击发布
    # driver.find_element(By.XPATH,'//*[text()="发布"]').click()


# 开始执行视频发布
publish_qutoutiao()
